package com.zxh.zhenxihuan.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.zxh.zhenxihuan.entity.z.OrderForm;
import com.zxh.zhenxihuan.service.OrderFormService;
import com.zxh.zhenxihuan.util.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController

@RequestMapping("/orderForm")
public class OrderFormController {
    @Resource
    private OrderFormService orderFormService;

    /**
     * 订单查看所有
     */

    @PostMapping("/findAll")
    public String findAll(@RequestBody Map maps){
        try {
            Integer num = (Integer) maps.get("page");
            Integer count = (Integer) maps.get("limit");
            Page<OrderForm> pages = orderFormService.page(num, count);
            Map<String, Object> map = new HashMap<>();
            map.put("page", pages);
            System.out.println(pages);
            return JSON.toJSONStringWithDateFormat(map, "yyyy-MM-dd", SerializerFeature.DisableCircularReferenceDetect);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 订单删除
     */
    @PostMapping("/orderFormDel")
    public String OrderFormDel(@RequestBody Map maps) {
        Integer id = (Integer) maps.get("id");
        Integer num = orderFormService.del(id);
        Map<String, Object> map = new HashMap<>();
        if (num > 0) {
            System.out.println("刪除成功");
        }
        return JSON.toJSONStringWithDateFormat(map, "yyyy-MM-dd HH:mm:ss", SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 订单添加
     */
    @ResponseBody
    @PostMapping("/orderFormAdd")
    public String OrderFormAdd(OrderForm orderForm) {
        System.out.println("orderFormAdd");
        OrderForm orderForm1 = orderFormService.addOrderForm(orderForm);
        System.out.println(orderForm1);
        return JSON.toJSONStringWithDateFormat(orderForm1, "yyyy-MM-dd HH:mm:ss", SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 订单修改
     */
    @PostMapping("/orderFormUpdate")
    public String OrderFormUpdate(@RequestBody OrderForm orderForm) {
        System.out.println("OrderFormUpdate");
        OrderForm orderForm1 = orderFormService.update(orderForm);
        Map<String, Object> map = new HashMap<>();
        map.put("orderForm", orderForm1);
        if (map.size() > 0) {
            System.out.println("修改成功");
            return JSON.toJSONStringWithDateFormat(orderForm, "yyyy-MM-dd HH:mm:ss", SerializerFeature.DisableCircularReferenceDetect);
        }
        return null;
    }
}
